package edu.uoc.tds.pec3.swingx;


import java.awt.Dimension;
import edu.uoc.tds.pec3.utils.MessageUtils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


import edu.uoc.tds.pec3.exception.GestorGastosException;
import edu.uoc.tds.pec3.server.Servidor;


//VS4E -- DO NOT REMOVE THIS LINE!
public class ConexionFrame extends JFrame implements ActionListener {


        private static final long serialVersionUID = 1L;
        private JLabel informacionLabel;
        private JButton iniciarButton;
        private JButton detenerButton;


        // private ConexionPanel conexionPanel;
        private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";


        public ConexionFrame() {
                initComponents();
        }


        private void initComponents() {
                MessageUtils.loadMessages("messages");
                setDefaultCloseOperation(ConexionFrame.EXIT_ON_CLOSE);
                getContentPane().setPreferredSize(this.getSize());
                pack();
                setLocationRelativeTo(null);


                setTitle(MessageUtils.getMessage("title"));
                setLayout(null);
                setSize(500, 100);
                setIconImages(null);
                setResizable(true);


                getContentPane().add(getInformacionLabel());
                getContentPane().add(getIniciarButton());
                getContentPane().add(getDetenerButton());


                setVisible(true);
        }


        private static void installLnF() {
                try {
                        String lnfClassname = PREFERRED_LOOK_AND_FEEL;
                        if (lnfClassname == null)
                                lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
                        UIManager.setLookAndFeel(lnfClassname);
                } catch (Exception e) {
                        System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                                        + " on this platform:" + e.getMessage());
                }
        }


        /**
         * Main entry of the class. Note: This class is only created so that you can
         * easily preview the result at runtime. It is not expected to be managed by
         * the designer. You can modify it as you like.
         */
        public static void main(String[] args) {
                installLnF();
                SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                                ConexionFrame frame = new ConexionFrame();


                        }
                });
        }


        private JLabel getInformacionLabel() {
                if (informacionLabel == null) {
                        informacionLabel = new JLabel();
                        informacionLabel.setText(MessageUtils.getMessage("label.informacion.servidorPendiente"));
                        informacionLabel.setBounds(153, 40, 280, 12);
                }
                return informacionLabel;
        }


        private JButton getIniciarButton() {
                if (iniciarButton == null) {
                        iniciarButton = new JButton();
                        iniciarButton.setText(MessageUtils.getMessage("boton.iniciarServidor"));
                        iniciarButton.setBounds(13, 10, 197, 24);
                        iniciarButton.addMouseListener(new MouseAdapter() {


                                public void mouseClicked(MouseEvent event) {
                                        if (iniciarButton.isEnabled())
                                                        iniciarServidor(event);
                                }
                        });
                }
                return iniciarButton;
        }


        private JButton getDetenerButton() {
                if (detenerButton == null) {
                        detenerButton = new JButton();
                        detenerButton.setText(MessageUtils.getMessage("boton.detenerServidor"));
                        detenerButton.setEnabled(false);
                        detenerButton.setBounds(213, 10, 197, 24);
                        detenerButton.addMouseListener(new MouseAdapter() {


                                public void mouseClicked(MouseEvent event) {
                                        if (detenerButton.isEnabled())
                                                detenerServidor(event);
                                }
                        });
                }
                return detenerButton;
        }


        private void iniciarServidor(MouseEvent event) {
                try {
                Servidor.iniciar();
                iniciarButton.setEnabled(false);
                detenerButton.setEnabled(true);
                informacionLabel.setText(MessageUtils.getMessage("label.informacion.servidorIniciado"));
                } catch (GestorGastosException ex) {
                JOptionPane.showMessageDialog(null, ex.getDescError() + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }


        }


        private void detenerServidor(MouseEvent event) {
                try {
                        Servidor.detener();     
                        detenerButton.setEnabled(false);
                        iniciarButton.setEnabled(true);
                        informacionLabel.setText(MessageUtils.getMessage("label.informacion.servidorPendiente"));
                } catch (GestorGastosException ex) {
                JOptionPane.showMessageDialog(null, ex.getDescError() + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }
        }


        @Override
        public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub


        }


}